package so.shanku.essential.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import net.tsz.afinal.json.JsonMap;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;
import so.shanku.essential.R;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.utils.ImageLoaderBitmapCallBack;
import so.shanku.essential.utils.StringUtil;
import so.shanku.essential.utils.Utils;

/**
 * Created by Jaelyn on 2015/9/9 0009.
 */
public class RelationProductAdapter extends MyBaseAdapter {

    /**
     * 是否显示描述字段 默认不显示
     */
    private boolean showDiscri = false;

    public RelationProductAdapter(Context context) {
        super(context);
    }

    /**
     * 是否显示描述信息
     *
     * @return
     */
    public boolean isShowDiscri() {
        return showDiscri;
    }

    public void setShowDiscri(boolean isShowDiscri) {
        this.showDiscri = isShowDiscri;
    }


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        RelationGoodsAdapterViewHolder relationGoodsAdapterViewHolder;
        if (view == null) {
            view = layoutInflater.inflate(R.layout.relationgoods_item, null);
            relationGoodsAdapterViewHolder = new RelationGoodsAdapterViewHolder();
            ViewUtils.inject(relationGoodsAdapterViewHolder, view);
            view.setTag(relationGoodsAdapterViewHolder);
        } else {
            relationGoodsAdapterViewHolder = (RelationGoodsAdapterViewHolder) view.getTag();
        }

        bindData(i, relationGoodsAdapterViewHolder);
        return view;
    }

    private void bindData(int i, RelationGoodsAdapterViewHolder relationGoodsAdapterViewHolder) {
        JsonMap jsonMap = (JsonMap) datas.get(i);
        String brandLogoPath = jsonMap.getStringNoNull("picPath");
        brandLogoPath = StringUtil.get_MImage(brandLogoPath);
        if (MyApplication.getInstance().getBitmapHashMap().containsKey(brandLogoPath)) {
            relationGoodsAdapterViewHolder.relation_goods_iv.setImageBitmap((MyApplication.getInstance().getBitmapHashMap().get(brandLogoPath)));
        } else {
            ImageLoader.getInstance().displayImage(brandLogoPath, relationGoodsAdapterViewHolder.relation_goods_iv, MyApplication.getInstance().getBrandDefaultOption(), new ImageLoaderBitmapCallBack());

            //bitmapUtils.display(holder.programIcon, imageurl, customBitmapLoadCallBack);
        }
        boolean isSelfProduct=jsonMap.getBoolean("productType");
        if(isSelfProduct){
            relationGoodsAdapterViewHolder.self_product_iv.setVisibility(View.VISIBLE);
        } else {
            relationGoodsAdapterViewHolder.self_product_iv.setVisibility(View.GONE);
        }
        relationGoodsAdapterViewHolder.goods_name_tv.setText(jsonMap.getStringNoNull("productName"));

        String marketPrice = jsonMap.getStringNoNull("marketPice");
        String discountPrice = jsonMap.getStringNoNull("salePrice");
        relationGoodsAdapterViewHolder.goods_price_tv.setText(StringUtil.getFormatMoneyWithSign(marketPrice));
        if (!TextUtils.isEmpty(discountPrice) && !discountPrice.equalsIgnoreCase("0") && !discountPrice.equalsIgnoreCase(marketPrice)) {
            relationGoodsAdapterViewHolder.goods_price_discount.setVisibility(View.VISIBLE);
            relationGoodsAdapterViewHolder.goods_price_discount.setText(StringUtil.getFormatMoneyWithSign(discountPrice));
            Utils.addDeleteLine(relationGoodsAdapterViewHolder.goods_price_tv);
            relationGoodsAdapterViewHolder.goods_price_discount.setSelected(true);
        } else {
            Utils.crashDeleteLine(relationGoodsAdapterViewHolder.goods_price_tv);
            relationGoodsAdapterViewHolder.goods_price_discount.setVisibility(View.GONE);
        }

        if (showDiscri) {
            relationGoodsAdapterViewHolder.goods_feature_discribe_tv.setText(jsonMap.getStringNoNull("brandName"));
        } else {
            relationGoodsAdapterViewHolder.goods_feature_discribe_tv.setVisibility(View.GONE);
        }
    }


    private class RelationGoodsAdapterViewHolder {
        @ViewInject(R.id.relation_goods_iv)
        public ImageView relation_goods_iv; //商品icon
        @ViewInject(R.id.self_product_iv)
        private TextView self_product_iv;//自营标记
        @ViewInject(R.id.product_name_tv)
        public TextView goods_name_tv;   //商品名字
        @ViewInject(R.id.brand_name_tv)
        public TextView goods_feature_discribe_tv; //描述
        @ViewInject(R.id.goods_price_tv)
        public TextView goods_price_tv; //原价
        @ViewInject(R.id.goods_price_discount_tv)
        public TextView goods_price_discount; //折扣价
    }

}
